package echat.future.cn.echat.my.view;

import android.annotation.SuppressLint;
import android.content.Intent;

import java.util.concurrent.TimeUnit;

import echat.future.cn.echat.MainActivity;
import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityBindPhoneBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.UserBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.PhoneUtils;
import echat.future.cn.echat.my.utils.RxUtils;
import echat.future.cn.echat.my.utils.ToastUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import lumingweihua.future.cn.baselibgxh.utils.MD5;

public class BindPhoneActivity extends BaseActivity<ActivityBindPhoneBinding>{

    @Override
    public int layoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle(this.getResources().getString(R.string.bind_title),16,R.color.color_333333).setBack();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        RxUtils.clickView(bind.register).subscribe(view -> bindPhone());
        RxUtils.clickView(bind.tvCode).subscribe(view -> getCode());
    }

    /**
     * 绑定手机号码成功后调到
     */
    private void bindPhone(){
        if(!PhoneUtils.isMobile0(bind.phone.getText().toString())){
            ToastUtils.toastMsg(this,"请输入正确的手机号码");
            return;
        }
        if(!PhoneUtils.isPass(bind.pass.getText().toString())){
            ToastUtils.toastMsg(this,"请输入正确的密码");
            return;
        }
        String md5Pass = MD5.Md5(bind.pass.getText().toString()).toUpperCase();
        Observable observable =
                HttpUtilsRequst.retrofitClient.create(UserApi.class).
                        bindPhone(UserHelper.getToken(),bind.phone.getText().toString(),
                                md5Pass,bind.code.getText().toString(),getIntent().getIntExtra("authType",1)
                        ,getIntent().getStringExtra("openId"))
                        .flatMap(tokenBaseVo -> {
                            //绑定成功，开始获取用户信息
                            if(tokenBaseVo.getCode() == 1){
                                String key = "9b4aaf?&&##664bdef@c3fc3efa9";
                                return HttpUtilsRequst.retrofitClient.create(UserApi.class).getUserInfo(UserHelper.getToken(),MD5.Md5(System.currentTimeMillis()/1000+key).toLowerCase());
                            }else{
                                throw new Exception(tokenBaseVo.getMsg());
                            }
                        });
        new HttpUtilsRequst<String>(this,observable).
                setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<UserBean>) o -> {
                    ToastUtils.toastMsg(this,"绑定手机号码成功");
                    UserHelper.save(o);
                    startActivity(new Intent(this,MainActivity.class));
                    finish();
                }).start();
    }



    /**
     * 获取手机号码
     */
    private void getCode(){
        //1校验手机号码
        if(!PhoneUtils.isMobile0(bind.phone.getText().toString())){
            toast("请输入正确的手机号码");
            return;
        }
        //禁止点击
        bind.tvCode.setClickable(false);
        Observable.interval(1, TimeUnit.SECONDS)
                .take(59)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        bind.tvCode.setClickable(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        bind.tvCode.setText("剩余" + (60 - aLong) + "秒");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        bind.tvCode.setClickable(true);
                        bind.tvCode.setText("获取验证码");
                    }
                });
        new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).sendCode(bind.phone.getText().toString())).
                setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("短信验证码已经发送到"+bind.phone.getText().toString());
                }).start();
    }

}
